package com.listen.start;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.listen.vo.XunwangVO;
import com.listen.model.User;
import com.listen.service.StudyService;
import com.listen.service.UserService;
import com.listen.service.DB.GanBuXueYuanStudy;
import com.listen.service.DB.ShuiliServiceImpl;
import com.listen.service.DB.XunwangServiceImpl;
import com.listen.task.ListenTask;

/**
 * 系统初始化监听，加载流程配置数据、字典数据等
 */
public class InitListener implements ServletContextListener {

    private static Log log = LogFactory.getLog(InitListener.class);
    private static UserService userService = null;
    private static StudyService studyService = null;
    public static long SECONDS_NUMBER = 899;
    @Override
    public void contextInitialized(ServletContextEvent sce) {
    	log.info("开始启动定时听课任务...");
    	userService = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()).getBean(UserService.class);
    	studyService = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()).getBean(StudyService.class);
    	try {
    		//八大员、三类、建造师
    		Thread thread1 = new Thread() {
    			public void run() {
    				log.info("30秒后启动一个轮询线程，每"+SECONDS_NUMBER+"秒轮询一次");
    				ListenTask task = new ListenTask(userService,studyService);
    				ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
    				timer.scheduleWithFixedDelay(task, 20, SECONDS_NUMBER, TimeUnit.SECONDS);
    			}
    			
    		};
    		thread1.start();
    		
    		/*//干部学院
    		Thread thread2 = new Thread() {
    			public void run() {
    				try {
    					ganbuxueyuan();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
    			}
    		};
    		thread2.start();
    		
    		//讯网教学云平台
    		Thread thread3 = new Thread() {
    			public void run() {
    				try {
						xunwang();
					} catch (Exception e) {
						e.printStackTrace();
					}
    			}
    		};
    		thread3.start();*/
    		
    		
    		//水利五大员
    		/*Thread thread4 = new Thread() {
    			public void run() {
    				try {
    					log.info("10秒后启动一个轮询线程，每2388秒轮询一次");
    					ShuiliTask task = new ShuiliTask(userService);
        				ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
        				timer.scheduleWithFixedDelay(task, 10, 2388, TimeUnit.SECONDS);
					} catch (Exception e) {
						e.printStackTrace();
					}
    			}
    		};
    		thread4.start();*/
    		
		} catch (Exception e) {
			log.error("初始化启动失败!",e);
		}
    }

    public static void ganbuxueyuan() throws InterruptedException {
    	User u = new User();
        u.setZy("G");
        List<User> userList = userService.selectByUser(u);
        for(User user : userList) {
     	   user.setAddress(user.getCompanyName());
     	   user.setCookie(user.getIp());
     	   GanBuXueYuanStudy thread = new GanBuXueYuanStudy(user);
     	   thread.start();
        }
    }
    
    public static void xunwang() throws Exception {
    	User u = new User();
        u.setZy("X");
        List<User> userList = userService.selectByUser(u);
        
        User usr = null;
        for(User user : userList) {
        	XunwangServiceImpl xunwang = new XunwangServiceImpl();
        	xunwang.indexPage(user, new XunwangVO());
        	usr = user;
        }
        int i = 1;
        while(true) {
        	Thread.sleep(1000);
        	i++;
			if(i%300 == 0 && usr.isFinished()) {
				i = 0;
				usr.setStatus(1);
				xunwang();
				break;
			}
		}
    }
    
    /**
     *	 水利5大员
     * <p>Title: shuili</p>  
     * <p>Description: </p>  
     * @throws Exception
     */
    public static void shuili() throws Exception {
    	User u = new User();
        u.setZy("SL");
        List<User> userList = userService.selectByUser(u);
        
        for(User user : userList) {
        	ShuiliServiceImpl shuili = new ShuiliServiceImpl();
        	Map<String,Object> map = shuili.lisetn(user, null);
        	int status = (int) map.get("successFlag");
        	int nowNum = (int) map.get("nowNum");
        	if(status == 1 && nowNum == user.getNowNum()) {
        		User usr = new User();
        		usr.setId(user.getId());
        		usr.setNowNum(15);
        		usr.setStatus(3);
        		userService.updateSelective(usr);
        	}
        }
    }

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub
		
	}
}
